/**
 * @file Bsp_Fan.c
 * @author 徐松亮 (5387603@qq.com)
 * @version 1.0
 * @date 2023-09-28
 *
 * @copyright Copyright (c) {2030}  沈阳迈思医疗科技有限公司
 *
 * @brief 驱动层-->直流风扇驱动(PWM控速+软件测速)
 */

/* 私有头部文件 (Includes)-----------------------------------------------------*/
#include <stdbool.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include "Bsp_Fan.h"
/* 编译警告条件 (Compile warning condition)------------------------------------*/
/* 私有类型定义 (Private typedef)----------------------------------------------*/
/* 私有型宏定义 (Private define)-----------------------------------------------*/
/* 私有常量定义 (Private constants)--------------------------------------------*/
/* 公有常量定义 (Public constants)---------------------------------------------*/
/* 私有变量定义 (Private variables)--------------------------------------------*/
/* 公有变量定义 (Public variables)---------------------------------------------*/
/* 私有函数声明 (Private function prototypes)----------------------------------*/
/* 私有函数实体 (Private functions)--------------------------------------------*/
/**
 * @brief   周期执行函数
 * @note    用于PwmIn
 * @param   None
 * @return  None
 */
static void BspFan_Cycle_1ms(void *_tp_interface)
{
    BSP_FAN_T_INTERFACE *tp_interface = _tp_interface;
    // 转速计算
    if (tp_interface->t_Input.fnp_ReadGpioPwm != NULL)
    {
        tp_interface->t_Private.us_Speed_Timer++;
        if (tp_interface->t_Private.us_Speed_Timer >= 1000)
        {
            tp_interface->t_Private.us_Speed_Timer = 0;
            tp_interface->t_Private.us_Speed_Hz = tp_interface->t_Private.us_Speed_Cmt;
            tp_interface->t_Private.us_Speed_Cmt = 0;
        }
        if (tp_interface->t_Input.fnp_ReadGpioPwm() == 0)
        {
            tp_interface->t_Private.uc_Speed_State = 0;
        }
        else
        {
            if (tp_interface->t_Private.uc_Speed_State == 0)
            {
                tp_interface->t_Private.us_Speed_Cmt++;
            }
            tp_interface->t_Private.uc_Speed_State = 1;
        }
    }
    else
    {
        tp_interface->t_Private.us_Speed_Hz = 0;
    }
    // 按照PWM输出(10ms做为一个周期，步长1ms)
    if (tp_interface->t_Private.uc_DebugTestEnable != 1)
    {
        tp_interface->t_Private.us_Pwm_Cmt += 1000;
        tp_interface->t_Private.us_Pwm_Cmt = tp_interface->t_Private.us_Pwm_Cmt % 10000;
        if (tp_interface->t_Private.us_Pwm_Cmt < tp_interface->t_Private.us_Pwm)
        {
            // 开启
            tp_interface->t_Input.fnp_WriteGpioPwm(true);
        }
        else
        {
            // 关闭
            tp_interface->t_Input.fnp_WriteGpioPwm(false);
        }
    }
}
/**
 * @brief   周期执行函数
 * @note    用于Debug测试等
 * @param   None
 * @return  None
 */
static void BspFan_Cycle_100ms(void *_tp_interface)
{
    char *pbuf = NULL;
    BSP_FAN_T_INTERFACE *tp_interface = _tp_interface;
    if (tp_interface->t_Private.uc_DebugTestEnable == 1)
    {
        if (tp_interface->t_Private.uc_DebugTestEnablebak == 0)
        {
            tp_interface->t_Private.uc_DebugTestEnablebak = 1;
        }
        tp_interface->t_Input.fnp_WriteGpioPwm(true);
        if (tp_interface->t_Private.uc_DebugPrintEnable == 1)
        { // 申请缓存
            pbuf = ModuleMemManager_Get(E_MEM_MANAGER_TYPE_256B);
            // 打印输出
            sprintf(pbuf, "DebugOut: Speed_Hz-%d\r\n", tp_interface->t_Private.us_Speed_Hz);
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
            // 释放缓存
            ModuleMemManager_Free(E_MEM_MANAGER_TYPE_256B, pbuf);
        }
    }
    else
    {
        if (tp_interface->t_Private.uc_DebugTestEnablebak == 1)
        {
            tp_interface->t_Private.uc_DebugTestEnablebak = 0;
            //
            if (tp_interface->t_Private.us_Pwm > 10000)
            {
                tp_interface->t_Private.us_Pwm = 10000;
            }
        }
    }
    // 开机自检
    {
        switch (tp_interface->t_Private.uc_Check_Step)
        {
        case 0: // 风扇开启
            BspFan_TestOnOff(true, tp_interface);
            tp_interface->t_Private.uc_Check_Cmt = 0;
            tp_interface->t_Private.uc_Check_Step++;
            break;
        case 1: // 等待3秒
            tp_interface->t_Private.uc_Check_Cmt++;
            if (tp_interface->t_Private.uc_Check_Cmt >= 30)
            {
                tp_interface->t_Private.uc_Check_Step++;
            }
            break;
        case 2: // 判断
            if (tp_interface->t_Private.us_Speed_Hz > 100)
            {
                tp_interface->t_Private.uc_Error = false;
            }
            else
            {
                tp_interface->t_Private.uc_Error = true;
            }
            tp_interface->t_Private.uc_Check_Step++;
            break;
        case 3: // 风扇关闭
            BspFan_TestOnOff(false, tp_interface);
            tp_interface->t_Private.uc_Check_Step++;
            break;
        default:
            break;
        }
    }
}
static uint16_t BspFan_R_Pwm(void *_tp_interface)
{
    BSP_FAN_T_INTERFACE *tp_interface = (BSP_FAN_T_INTERFACE *)_tp_interface;
    if (tp_interface == NULL)
    {
        return 0;
    }
    return tp_interface->t_Private.us_Pwm;
}
static void BspFan_W_Pwm(void *_tp_interface, uint16_t _us_pwm)
{
    BSP_FAN_T_INTERFACE *tp_interface = (BSP_FAN_T_INTERFACE *)_tp_interface;
    if (tp_interface == NULL)
    {
        return;
    }
    tp_interface->t_Private.us_Pwm = _us_pwm;
}
static uint16_t BspFan_R_Speed_Hz(void *_tp_interface)
{
    BSP_FAN_T_INTERFACE *tp_interface = (BSP_FAN_T_INTERFACE *)_tp_interface;
    if (tp_interface == NULL)
    {
        return 0;
    }
    return tp_interface->t_Private.us_Speed_Hz;
}
static uint8_t BspFan_R_Error(void *_tp_interface)
{
    BSP_FAN_T_INTERFACE *tp_interface = (BSP_FAN_T_INTERFACE *)_tp_interface;
    if (tp_interface == NULL)
    {
        return 0;
    }
    return tp_interface->t_Private.uc_Error;
}
/* 公有函数实体 (Public functions)---------------------------------------------*/
/**
 * @brief   初始化函数
 * @note    用于初始化变量
 * @param   None
 * @return  None
 */
void BspFan_Init(BSP_FAN_T_INTERFACE *_tp_interface)
{
    _tp_interface->t_Transplant.fnp_Cycle_1ms = BspFan_Cycle_1ms;
    _tp_interface->t_Transplant.fnp_Cycle_100ms = BspFan_Cycle_100ms;
    //
    _tp_interface->t_Output.fnp_R_Pwm = BspFan_R_Pwm;
    _tp_interface->t_Output.fnp_W_Pwm = BspFan_W_Pwm;
    _tp_interface->t_Output.fnp_R_Speed_Hz = BspFan_R_Speed_Hz;
    _tp_interface->t_Output.fnp_R_Error = BspFan_R_Error;
}
/**
 * @brief   Debug接口函数
 * @note    开启Debug相关指令后会循环打印
 * @param   OnOff-判断指令进入与退出
 * @return  None
 */

void BspFan_DebugTestOnOff(uint8_t OnOff, void *_tp_interface)
{
    BSP_FAN_T_INTERFACE *tp_interface = (BSP_FAN_T_INTERFACE *)_tp_interface;
    if (tp_interface == NULL)
    {
        return;
    }
    if (OnOff == true)
    {
        tp_interface->t_Private.uc_DebugTestEnable = 1;
        tp_interface->t_Private.uc_DebugPrintEnable = 1;
    }
    else
    {
        tp_interface->t_Private.uc_DebugTestEnable = 0;
        tp_interface->t_Private.uc_DebugPrintEnable = 0;
    }
}

void BspFan_TestOnOff(uint8_t OnOff, void *_tp_interface)
{
    BSP_FAN_T_INTERFACE *tp_interface = _tp_interface;
    if (tp_interface == NULL)
    {
        return;
    }
    BspFan_DebugTestOnOff(OnOff, _tp_interface);
    tp_interface->t_Private.uc_DebugPrintEnable = 0;
}

/********************************END OF FILE***********************************/
